<template>
  <div class="app-container">
    <eHeader
      ref="header"
      :query="query"
      :eqpt-id="eqptId"
      :sup-this="supThis"
      :brand-list="eqptBrandList"
      :model-list="eqptModelnameList"
    />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <!--<el-table-column prop="id" label="序号" />-->
      <el-table-column :show-overflow-tooltip="true" prop="spareno" label="备件编号" />
      <el-table-column :show-overflow-tooltip="true" prop="sparepartname" label="名称" />
      <el-table-column :show-overflow-tooltip="true" prop="typename" label="类型" />
      <el-table-column :show-overflow-tooltip="true" prop="sparepartbrand" label="品牌" :formatter="formatBrand" />
      <el-table-column :show-overflow-tooltip="true" prop="spareparttmodel" label="型号" :formatter="formatModel" />
      <el-table-column :show-overflow-tooltip="true" prop="serialno" label="序列号" />
      <el-table-column :show-overflow-tooltip="true" prop="sparebelong" label="备件归属" />
      <el-table-column :show-overflow-tooltip="true" prop="warehouse" label="存放仓库" />
      <el-table-column :show-overflow-tooltip="true" prop="supplier" label="供应商" />
      <el-table-column :show-overflow-tooltip="true" prop="stock_by" label="入库人" />
      <el-table-column :show-overflow-tooltip="true" prop="stock_time" label="入库时间">
        <template slot-scope="scope">
          <span>{{ parseTimeShort(scope.row.stock_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="state" label="状态" :formatter="formatState" />
      <el-table-column v-if="checkPermission(['ADMIN','TSPAREPART_ALL','TSPAREPART_EDIT','TSPAREPART_DELETE'])" label="操作" width="160px" align="center" fixed="right">
        <template slot-scope="scope">
          <edit v-permission="['ADMIN','TSPAREPART_ALL','TSPAREPART_EDIT']" :data="scope.row" :sup-this.sync="supThis" :is-view="true" :is-add="false" />
          <sparepartqrcode v-permission="['ADMIN','TSPAREPART_ALL','TSPAREPART_EDIT']" :data="scope.row" :sup-this="supThis" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { parseTimeShort } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import sparepartqrcode from './module/sparepartqrcode'
import { getDictDetails, getDictLabel } from '@/api/dictDetail'
import { SPARE_STATES } from '@/constants/consts'
export default {
  components: { eHeader, edit, sparepartqrcode },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      supThis: this,
      eqptId: 0,
      eqptBrandList: [],
      eqptModelnameList: []
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTimeShort,
    checkPermission,
    beforeInit() {
      this.url = 'api/tSparepart'
      const sort = 'spareno,asc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      // const type = query.type
      // const value = query.value
      // get brand
      getDictDetails('sparepart_brand').then(res => {
        this.eqptBrandList = res.content
      }).catch(err => {
        console.log(err)
        return false
      })
      // get model
      getDictDetails('sparepart_modelname').then(res => {
        this.eqptModelnameList = res.content
      }).catch(err => {
        console.log(err)
        return false
      })

      if (this.eqptId) {
        this.params['eqpt_id'] = this.eqptId
      }
      if (query.typename) {
        this.params['typename'] = query.typename
      }
      if (query.sparepartname) {
        this.params['sparepartname'] = query.sparepartname
      }
      if (query.spareparttmodel) {
        this.params['spareparttmodel'] = query.spareparttmodel
      }
      if (query.sparepartbrand) {
        this.params['sparepartbrand'] = query.sparepartbrand
      }
      if (query.state) {
        this.params['state'] = query.state
      }
      return true
    },
    formatBrand(row) {
      return getDictLabel(row['sparepartbrand'], this.eqptBrandList)
    },
    formatModel(row) {
      return getDictLabel(row['spareparttmodel'], this.eqptModelnameList)
    },
    formatState(row) {
      for (var index in SPARE_STATES) {
        const obj = SPARE_STATES[index]
        if (obj.id === row['state']) {
          return obj.name
        }
      }
      return row['state']
    }
  }
}
</script>

<style scoped>

</style>
